;;;;  $Id: expand.sld 44 2012-03-28 06:25:03Z alan.watson.f@gmail.com $
;;;;
;;;;  Copyright (c) Dorai Sitaram 1999-2010.
;;;;  Copyright (c) Alan Watson 2012.
;;;;
;;;; This program is licensed under version 3 of the GNU LGPL. See
;;;; http://www.gnu.org/licenses/lgpl.html.

(define-library (scheme-indent expand)
  
  (export expand)
  
  (import (scheme base))
  (import (scheme read))
  (import (scheme write))
  
  (begin
    
    (define (expand tab-width)
      ;; The expand procedure filters copies the current input to the
      ;; current output, expanding tabs to multiples of tab-width
      ;; columns.
      (let char-loop ((i 0))
        (let ((c (read-char)))
          (cond
           ((eof-object? c))
           ((char=? c #\tab)
            (let tab-loop ((i (+ i 1)))
              (write-char #\space)
              (if (zero? (remainder i tab-width))
                (char-loop i)
                (tab-loop (+ i 1)))))
           ((or (char=? c #\newline)
                (char=? c #\return))
            (write-char c)
            (char-loop 0))
           (else
            (write-char c)
            (char-loop (+ i 1)))))))))
